package atguigu.nio;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Author: DRevelation
 * @Date: 2021/3/16 14:09
 * NIO写文件
 */
public class NIOFileChannel01 {
    public static void main(String[] args) throws IOException {
        String str = "Hello, 尚硅谷";
        try (FileOutputStream fileOutputStream = new FileOutputStream("D:\\nio_01.txt")) {

            FileChannel fileChannel = fileOutputStream.getChannel();
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            byteBuffer.put(str.getBytes());
            // 从写模式转成读模式
            byteBuffer.flip();

            fileChannel.write(byteBuffer);
        }
    }
}
